<?php

namespace App\Models;

use App\Util\Url;
use Illuminate\Database\Eloquent\Model;

class StoreOrder extends Model
{
    protected $table = "c_store_orders";

    public function work()
    {
        return $this->hasOne('App\Models\DoctorWorks', 'id', 'work_id');
    }

    /**
     * @param $filter
     * @return mixed
     * 获取打赏订单列表
     */
    public function getRewardOrderList($filter)
    {
        $where = [];
        if(isset($filter['user_id'])){
            $where[] = ['user_id','=',$filter['user_id']];
        }
        if (isset($filter['work_id'])) {
            $where[] = ['work_id', '=', $filter['work_id']];
        }
        if (isset($filter['doctor_id'])) {
            $where[] = ['store_id', '=', $filter['doctor_id']];
        }
        if (isset($filter['status'])) {
            if($filter['status'] == 3){
                $where[] = ['order_status', '=', 0];
            }else{
                $where[] = ['order_status', '=', $filter['status']];
            }
        }
        $where[] = ['pay_status', '=', 1];
        $re_data = $this->whereIn("type",[10,20])->where($where)->with(['work'])->orderByDesc("id")->simplePaginate()->toArray();
        foreach ($re_data['data'] as $k=>&$v){
            if($v['type']==10){
                $info = (new Doctor())->where([['id','=',$v['store_id']]])->first()->toArray();
                $info['profile'] = Url::getHttpHost().'upload/'. $info['profile'];
                $v['info'] = $info;
            }elseif ($v['type']==20){
                $info = (new DoctorWorks())->where([['id','=',$v['store_id']]])->first()->toArray();
                $info['logo'] = Url::getHttpHost().'upload/'. $info['logo'];
                $v['info'] = $info;
            }
            if($v['type_id'] >0){
                 $v['goods'] = (new Reward())->where([['id','=',$v['type_id']]])->first()->toArray();
            }else{
                  $v['goods']  = ['money'=>$v['money'],'rew_name'=>'自定义金额'];
            }
        }
        return $re_data;
    }
}
